package com.qidian.hw.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qidian.hw.pojo.Product;
import com.qidian.hw.service.ProductService;
import com.qidian.hw.service.impl.ProductServiceImpl;
import com.qidian.hw.tools.Const;
import com.qidian.hw.tools.JSONTools;
import com.qidian.hw.tools.dto.Pager;

/**
 * @author 郭浩
 *
 * @company 起点编程
 *
 *          2022年8月19日 下午7:12:51
 */
public class ProductServlet extends HttpServlet {
	ProductService productService = new ProductServiceImpl();

	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 处理POST请求的中文乱码问题
		if (req.getMethod().equalsIgnoreCase("POST")) {
			req.setCharacterEncoding("utf-8");
		}
		String op = req.getParameter("op");
		switch (op) {
		// 保存
		case "save":
			save(req, resp);
			break;
		// 回显
		case "input":
			input(req, resp);
			break;
		// 表单传回值修改
		case "update":
			update(req, resp);
			break;
		// 列表
		case "list":
			list(req, resp);
			break;
		// 删除
		case "delete":
			delete(req, resp);
			break;
		case "deletes":
			deletes(req, resp);
			break;
		case "query":
			query(req,resp);
			break;
		default:
			super.service(req, resp);
		}
	}

	
	public void deletes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String result = "0";
		String [] ids = req.getParameterValues("productId");
		if(ids!=null && ids.length > 0) {
			result = productService.deletes(ids) + "";
		}
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result);
	}
	
	public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String productTitle = req.getParameter("productTitle");
		String productDesc = req.getParameter("productDesc");
		String solutionImg = req.getParameter("solutionImg");
		String productHref = req.getParameter("productHref");
		String typeDesc = req.getParameter("typeDesc");
		Product product = new Product(productTitle, productDesc, solutionImg, productHref, typeDesc);
		int result = productService.productSave(product);
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result + "");
	}

	public void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String productIdStr = req.getParameter("productId");
		long productId = 0;
		try {
			productId = Long.parseLong(productIdStr);
		} catch (Exception e) {
		}
		Product product = productService.queryProductById(productId);
		String productStr = JSONTools.object2json(product);
		resp.setContentType(Const.RES_JSON);
		resp.getWriter().write(productStr);
	}

	public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String productIdStr = req.getParameter("productId");
		long productId = 0;
		try {
			productId = Long.parseLong(productIdStr);
		}catch(Exception e) {}
		String productTitle = req.getParameter("productTitle");
		String productDesc = req.getParameter("productDesc");
		String solutionImg = req.getParameter("solutionImg");
		String productHref = req.getParameter("productHref");
		String typeDesc = req.getParameter("typeDesc");
		Product product = new Product(productTitle, productDesc, solutionImg, productHref, typeDesc);
		product.setProductId(productId);
		int result = productService.productUpdate(product);
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result + "");
	}

	public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ArrayList<Product> al = new ArrayList<Product>();
		al = productService.productList();
		String productsStr = JSONTools.object2json(al);
		resp.setContentType(Const.RES_JSON);
		resp.getWriter().write(productsStr);
	}

	public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String productIdStr = req.getParameter("productId");
		long productId = 0;
		try {
			productId = Long.parseLong(productIdStr);
		} catch (Exception e) {
		}
		int result = productService.productDelete(productId);
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result + "");
	}
	
	public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String title = req.getParameter("title");
		String pageStr = req.getParameter("page");
		String sizeStr = req.getParameter("size");
		int page = 1;
		int size = 10;
		try {
			page = Integer.parseInt(pageStr);
			size = Integer.parseInt(sizeStr);
		} catch (Exception e) {
		}
		Pager pager = productService.queryByPage(page, size, title);
		String pagerStr = JSONTools.object2json(pager);
		resp.setContentType(Const.RES_JSON);
		resp.getWriter().write(pagerStr);
	}
}
